import { Service } from '../../services/service'
import { formaturl } from '@/utils/commonUtils'
import dayjs from 'dayjs'
const state = {
  pagnation: {
    pageSize: 10,
    pageNum: 1,
    total: 0
  },
  queryUrlsMapping: {
    1: '/hanyun/app-service/api/v1/app/query/check/list',
    2: '/hanyun/app-service/api/v1/service/query/check/list',
    3: '/hanyun/app-service/api/v1/model/query/check/list'
  },
  auditUrlsMapping: {
    1: '/hanyun/app-service/api/v1/app/modify/status',
    2: '/hanyun/app-service/api/v1/service/modify/status',
    3: '/hanyun/app-service/api/v1/model/modify/status'
  },
  tableTab: '1',
  pageData: []
}
const getters = {}
const mutations = {
  setPageData(state, data) {
    state.pageData = data
  },
  setPagnation(state, data) {
    state.pagnation = Object.assign({}, state.pagnation, data)
  },
  setTableTab(state, data) {
    state.tableTab = data
  }
}
const actions = {
  async getList({ state, commit }) {
    const retData = await Service({
      url: state.queryUrlsMapping[state.tableTab] + formaturl(state.pagnation),
      method: 'get'
    })
    if (retData.status === 200) {
      const data = retData.result.data
      const result = retData.result
      data.forEach((e) => {
        e.createDate = dayjs(e.createDate).format('YYYY-MM-DD HH:mm:ss')
      })
      commit('setPageData', data)
      commit('setPagnation', {
        pageSize: result.pageSize,
        pageNum: result.pageNum,
        total: +result.total
      })
    }
  },
  async audit({ state }, params) {
    const retData = await Service({
      url: state.auditUrlsMapping[state.tableTab] + formaturl(params),
      method: 'post'
    })
    return new Promise((resolve, reject) => {
      if (retData.status === 200) {
        resolve('审核成功')
      } else {
        reject(retData.msg)
      }
    })
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
